<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace yiike\core\widgets;

use Yii;
/**
 * Description of Notice
 *
 * @author Sylar
 */
class Notice extends \yii\bootstrap\Widget{
    
    public $limit=5;
    
    public $private = false;
    
    public function run() {
        
        $query = \yiike\core\models\Notice::find();
        
        if ($this->private) {
            $count =$query->where("user_id=:uid AND status=1 AND is_read=0", [':uid' => Yii::$app->user->id])->count();
            $models = array();
            if ($count > 0) {
                $models = $query->where("user_id=:uid AND status=1 AND is_read=0", [':uid' => Yii::$app->user->id])->orderBy('created_at desc')->limit($this->limit)->all();
            }
        }
        else {
            $read = \yiike\core\models\NoticeRead::findOne(\Yii::$app->user->id);
            
            $count =$query->where("user_id=0 AND status=1 AND created_at > :time", [':time' => $read->read_at])->count();
            $models = array();
            if ($count > 0) {
                $models = $query->where("user_id=0 AND status=1 AND created_at > :time", [':time' => $read->read_at])->orderBy('created_at desc')->limit($this->limit)->all();
            }
        }
        return $this->render('notice', ['count' => $count, 'models' => $models, 'private' => $this->private]);
    }
}
